package com.hangzhou.xyj.ui.adapter;

import android.content.Intent;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.ViewGroup;

import com.hangzhou.xyj.R;
import com.hangzhou.xyj.manager.IntentManager;
import com.hangzhou.xyj.model.ActionModel;
import com.hangzhou.xyj.model.BannerModel;
import com.hangzhou.xyj.model.type.EIntentTagType;
import com.hangzhou.xyj.ui.activity.WebActivity;
import com.hangzhou.xyj.ui.adapter.base.BaseLoadingAdapter;
import com.hangzhou.xyj.ui.widget.FamousExpertBannerView;
import com.hangzhou.xyj.ui.widget.views.MyImageView;
import com.hangzhou.xyj.ui.widget.views.MyTextView;
import com.hangzhou.xyj.utils.constants.DataKey;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by DT-038 on 2015/12/22.
 * 热点adapter
 */
public class HotspotAdapter extends BaseLoadingAdapter<ActionModel> {
    private static final int TYPE_BANNER = 0;//轮播图
    private static final int TYPE_SINGLE_IMG = 1;//单图条目
    private static final int TYPE_DOUBLE_IMG = 2;//两图条目
    private static final int TYPE_TRIPLE_IMG = 3;//三图条目
    private List<BannerModel> mBannerList;

    public HotspotAdapter(RecyclerView recyclerView, List<ActionModel> actionList) {
        super(recyclerView, actionList);
    }

    /**
     * 更新banner条
     *
     * @param bannerList
     */
    public void updateBanner(List<BannerModel> bannerList) {
        if (bannerList == null) {
            bannerList = new ArrayList<>();
        }
        this.mBannerList = bannerList;
        notifyDataSetChanged();
    }


    @Override
    public void onBindNormalViewHolder(RecyclerView.ViewHolder viewHolder, final int position) {
        if (viewHolder == null) {
            return;
        }
        int viewType = getItemViewType(position);
        switch (viewType) {
            case TYPE_BANNER:
                ((BannerViewHolder) viewHolder).setData();
                break;
            case TYPE_SINGLE_IMG:
                ((HotspotSingleImgViewHolder) viewHolder).setData(mDataList.get(position - 1));
                ((HotspotSingleImgViewHolder) viewHolder).itemView.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        Intent intent = new Intent(mContext, WebActivity.class);
                        intent.putExtra(DataKey.TITLE, "头条详情");
                        intent.putExtra(DataKey.OBJ_ID, mDataList.get(position - 1).getId());
                        intent.putExtra(DataKey.INDEX, mDataList.get(position - 1).getUrl());
                        intent.putExtra(DataKey.INTENT_TAG, EIntentTagType.TAG_HOT_SPOT.getValue());
                        intent.putExtra(DataKey.SHARE_TITLE, mDataList.get(position - 1).getName());
                        intent.putExtra(DataKey.SHARE_CONTENT, mDataList.get(position - 1).getIntroduce());
                        intent.putExtra(DataKey.SHARE_URL, mDataList.get(position - 1).getShareUrl());
                        IntentManager.getInstance().goActivity(mContext, intent);
                    }
                });
                break;
            case TYPE_DOUBLE_IMG:
            case TYPE_TRIPLE_IMG:
            default:
                ((HotspotMuchImgViewHolder) viewHolder).setData(mDataList.get(position - 1));
                ((HotspotMuchImgViewHolder) viewHolder).itemView.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        Intent intent = new Intent(mContext, WebActivity.class);
                        intent.putExtra(DataKey.TITLE, "头条详情");
                        intent.putExtra(DataKey.OBJ_ID, mDataList.get(position - 1).getId());
                        intent.putExtra(DataKey.INTENT_TAG, EIntentTagType.TAG_HOT_SPOT.getValue());
                        intent.putExtra(DataKey.INDEX, mDataList.get(position - 1).getUrl());
                        intent.putExtra(DataKey.SHARE_CONTENT, mDataList.get(position - 1).getIntroduce());
                        intent.putExtra(DataKey.SHARE_URL, mDataList.get(position - 1).getShareUrl());
                        intent.putExtra(DataKey.SHARE_TITLE, mDataList.get(position - 1).getName());
                        IntentManager.getInstance().goActivity(mContext, intent);
                    }
                });
        }
    }

    @Override
    public RecyclerView.ViewHolder onCreateNormalViewHolder(ViewGroup viewGroup, int viewType) {
        RecyclerView.ViewHolder viewHolder;
        switch (viewType) {
            case TYPE_BANNER://轮播图
                viewHolder = new BannerViewHolder(mLayoutInflater.inflate(R.layout.layout_banner, viewGroup, false));
                break;
            case TYPE_SINGLE_IMG://单图条目
                viewHolder = new HotspotSingleImgViewHolder(mLayoutInflater.inflate(R.layout.layout_hotspot_item_single_img, viewGroup, false));
                break;
            case TYPE_DOUBLE_IMG://两图条目
            case TYPE_TRIPLE_IMG://三图条目
            default:
                viewHolder = new HotspotMuchImgViewHolder(mLayoutInflater.inflate(R.layout.layout_hotspot_item_much_img, viewGroup, false));
                break;
        }
        return viewHolder;
    }


    @Override
    public int getItemViewType(int position) {
        int imgSize;
        if (position == 0) {
            return TYPE_BANNER;
        }
        if (mDataList.get(position - 1) == null) {
            return TYPE_LOADING_ITEM;
        } else {
            imgSize = mDataList.get(position - 1).getImgList().size();
            if (imgSize == TYPE_SINGLE_IMG) {
                return TYPE_SINGLE_IMG;
            } else if (imgSize == TYPE_DOUBLE_IMG)
                return TYPE_DOUBLE_IMG;
            else
                return TYPE_TRIPLE_IMG;
        }
    }

    @Override
    public int getItemCount() {
        if (mDataList == null) {
            return 1;
        }
        setAdditionalSize(1);
        return mDataList.size() + 1;
    }

    class BannerViewHolder extends RecyclerView.ViewHolder {
        private FamousExpertBannerView mItemView;

        public BannerViewHolder(View itemView) {
            super(itemView);
            initView(itemView);
        }

        public void initView(View view) {
            mItemView = (FamousExpertBannerView) view.findViewById(R.id.view_famous_expert_banner);
        }

        public void setData() {
            mItemView.setData(mBannerList);
        }
    }

    class HotspotSingleImgViewHolder extends RecyclerView.ViewHolder {
        MyImageView mIvMainImg;//左边的图片
        MyTextView mTvTitle;//标题
        MyTextView mTvContent;//内容
        MyTextView mTvCommentNum;//评论数量
        MyTextView mTvLocation;//地点
        MyTextView mTvTime;//时间

        public HotspotSingleImgViewHolder(View itemView) {
            super(itemView);
            mIvMainImg = (MyImageView) itemView.findViewById(R.id.iv_hotspot_item_img);
            mTvTitle = (MyTextView) itemView.findViewById(R.id.tv_hotspot_item_title);
            mTvContent = (MyTextView) itemView.findViewById(R.id.tv_hotspot_item_desc);
            mTvCommentNum = (MyTextView) itemView.findViewById(R.id.mtv_comment_num);
            mTvLocation = (MyTextView) itemView.findViewById(R.id.mtv_location);
            mTvTime = (MyTextView) itemView.findViewById(R.id.mtv_time);
        }

        public void setData(ActionModel data) {
            //设置图片
            mIvMainImg.displayImage(data.getImgList().get(0));
            //设置标题
            mTvTitle.setText(data.getName());
            //设置介绍
            mTvContent.setText(data.getIntroduce());
            //设置评论数量
            mTvCommentNum.setText("" + data.getCommentCount());
            //设置地点
            mTvLocation.setText(data.getLocation());
            //设置时间
            mTvTime.setText(data.getTime());
        }
    }

    class HotspotMuchImgViewHolder extends RecyclerView.ViewHolder {
        MyImageView mIvImg1;//图片1
        MyImageView mIvImg2;//图片2
        MyImageView mIvImg3;//图片3
        MyTextView mTvTitle;//标题
        MyTextView mTvIntroduce;//介绍
        MyTextView mTvCommentNum;//评论数量
        MyTextView mTvLocation;//地点
        MyTextView mTvTime;//时间

        public HotspotMuchImgViewHolder(View itemView) {
            super(itemView);
            mIvImg1 = (MyImageView) itemView.findViewById(R.id.miv_image1);
            mIvImg2 = (MyImageView) itemView.findViewById(R.id.miv_image2);
            mIvImg3 = (MyImageView) itemView.findViewById(R.id.miv_image3);
            mTvTitle = (MyTextView) itemView.findViewById(R.id.mtv_title);
            mTvIntroduce = (MyTextView) itemView.findViewById(R.id.mtv_introduce);
            mTvCommentNum = (MyTextView) itemView.findViewById(R.id.mtv_comment_num);
            mTvLocation = (MyTextView) itemView.findViewById(R.id.mtv_location);
            mTvTime = (MyTextView) itemView.findViewById(R.id.mtv_time);
        }

        public void setData(ActionModel data) {
            //设置标题
            mTvTitle.setText(data.getName());
            //设置介绍
            mTvIntroduce.setText(data.getIntroduce());
            //设置评论数量
            mTvCommentNum.setText("" + data.getCommentCount());
            //设置地点
            mTvLocation.setText(data.getLocation());
            //设置时间
            mTvTime.setText(data.getTime());
            //设置图片
            int imgCount = data.getImgList().size();
            mIvImg1.displayImage(data.getImgList().get(0));
            mIvImg2.displayImage(data.getImgList().get(1));
            if (imgCount == 3) {//如果是三张图片
                mIvImg3.displayImage(data.getImgList().get(2));
            } else {
                mIvImg3.setVisibility(View.GONE);
            }
        }
    }

}
